package com.yanqu.road.server.manger.cricket.skillhandler.startround.handler;

import com.yanqu.road.pb.cricket.CricketProto;
import com.yanqu.road.server.gameplayer.module.cricket.battle.CricketGame;
import com.yanqu.road.server.gameplayer.module.cricket.model.BattleData;
import com.yanqu.road.server.gameplayer.module.cricket.model.SkillData;
import com.yanqu.road.server.manger.cricket.skillhandler.startround.CricketSkillStartRoundStrategy;
import com.yanqu.road.utils.RandomHelper;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class CricketSkill259Strategy implements CricketSkillStartRoundStrategy {

    /**
     * 技能处理-善有善报
     * @param cricketGame 本局对局对象
     * @param skillData 技能数据
     * @param action 我方
     * @param target 对方
     * @param roundId 第几回合
     * @param attackRoundBuilder 返回PB结构体
     * @return
     */
    @Override
    public boolean skillHandle(CricketGame cricketGame, SkillData skillData, BattleData action, BattleData target, int roundId, CricketProto.CricketAttackRound.Builder attackRoundBuilder) {
        //判断是否触发
        int useRate = skillData.getSkillEffect().getP3();
        int realRate = RandomHelper.rand1000();
        if (realRate < useRate) {
            //每回合为血量最少的单位恢复九色鹿攻击力N1千分比的血量
            CricketProto.CricketBattleFrame.Builder newBattleFrameBuilder = cricketGame.newBattleFrameBuilder();
            newBattleFrameBuilder.setActionUserId(action.getUserId());
            newBattleFrameBuilder.setSkillId(skillData.getSkillId());
            //恢复的血量
            long addHp = BigDecimal.valueOf(action.getAttack(target)).multiply(BigDecimal.valueOf(skillData.getSkillEffect().getP1())).divide(BigDecimal.valueOf(1000), 8, RoundingMode.DOWN).longValue();
            //双方都回血
            action.addHp(addHp);
            target.addHp(addHp);
            newBattleFrameBuilder.setAttack(addHp);
            newBattleFrameBuilder.setExt("1");
            cricketGame.fillRemainHp(newBattleFrameBuilder);
            attackRoundBuilder.addStartRound(newBattleFrameBuilder);
        }
        return false;
    }
}
